package com.pig4cloud.pig.goods.controller;

import com.pig4cloud.pig.goods.utils.QiniuCloudUtil ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

/**
 * 七牛云文件上传以及路径返回
 * @author : gaofee
 * @date : 13:03 2021/1/17
 * @码云地址 : https://gitee.com/itgaofee
 */
@Controller
@CrossOrigin
public class UploadController {


    @ResponseBody
    @RequestMapping(value="/uploadImg", method= RequestMethod.POST)
    public String uploadImg(MultipartFile image, HttpServletRequest request) {
        if (image!=null&&image.isEmpty()) {

            return "文件为空，请重新上传";
        }

        try {
            byte[] bytes = image.getBytes();
            String imageName = UUID.randomUUID().toString();

            QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
            try {
                //使用base64方式上传到七牛云
                String url = qiniuUtil.put64image(bytes, imageName);
                System.out.println(url);
                return url;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "啥都没有";
        } catch (IOException e) {

            return "出异常了";
        }
    }

}
